package com.woniuxy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.MerchantCommissionMapper;
import com.woniuxy.dao.OverOrderMapper;
import com.woniuxy.dao.PreOrderMapper;
import com.woniuxy.dto.DataGridView;
import com.woniuxy.dto.MyDto;
import com.woniuxy.pojo.MerchantCommission;
import com.woniuxy.pojo.OverOrder;
import com.woniuxy.pojo.PreOrder;
import com.woniuxy.pojo.User;
import com.woniuxy.service.OrderService;
import com.woniuxy.vo.OverOrderVo;
import com.woniuxy.vo.PreOrderVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 通过pid 查询停车位信息 num订单编号  createTime endtime模糊查询  muid 商家车位iD
     * @param preOrderVo
     * @return
     */
    @GetMapping("/orders")
    @ApiOperation(value = "查询停车位信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = DataGridView.class,notes = "查询停车位信息")
    @ResponseBody
    public DataGridView orders( PreOrderVo preOrderVo){

        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        preOrderVo.setPid(user.getId());
        return orderService.findAllorders(preOrderVo);
    }

    /**
     *超时订单
     * @param overOrderVo
     * @return
     */
    @GetMapping("/overorders")
    @ApiOperation(value = "超时订单",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = DataGridView.class,notes = "超时订单")
    @ResponseBody
    public DataGridView overorders( OverOrderVo overOrderVo){

        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        overOrderVo.setUid(user.getId());
        return orderService.findAlloverorders(overOrderVo);
    }

    /**
     * 查询商家用户收入
     * @param
     * @return
     */
    @GetMapping("/odersmoney")
    @ApiOperation(value = "查询商家用户收入",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
       notes = "查询商家用户收入")
    @ResponseBody
    public String odersmoner(){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        List<BigDecimal> lists=orderService.findMerchantusersMoney(user.getId());
        BigDecimal res=new BigDecimal(0);
        for (BigDecimal list : lists) {
         res=   res.add(list);
        }
        return res.toString();
    }
    @GetMapping("/odersmoney1")
    @ApiOperation(value = "查询商家全部提成",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            notes = "查询商家全部提成")
    @ResponseBody
    public String odersmoner1(){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        List<BigDecimal> lists=orderService.findMerchantusersMoney(user.getId());
        BigDecimal res=new BigDecimal(0);
        for (BigDecimal list : lists) {
          res=  res.add(list);
        }
        BigDecimal iterm=new BigDecimal(lists.size());
        res=res.divide(iterm);
        System.out.println(res);
        return res.toString();
    }
    @GetMapping("/findpreorder.do")
    @ApiOperation(value = "查询商家超时订单",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
           notes = "查询商家超时订单")
    @ResponseBody
    public MyDto findpreorder(@ApiParam(required = true,name = "orderid",value = "订单编号")String orderid){
        MyDto myDto=new MyDto();
    try {
        PreOrder preOrder = orderService.findpreorder(orderid);
        Boolean res =orderService.findOverOrder(orderid);
        if (res){
            myDto.setMsg("超时订单");
        }else {
            myDto.setMsg("未超时订单");
        }
        myDto.setCode(200);
        myDto.setData(preOrder);
        }catch (Exception e){
        e.printStackTrace();
        myDto.setCode(-200);
        myDto.setMsg("查询失败");
    }
       return myDto;
    }

}
